/*
 * Copyright 2003-2007 (c) Live Media Pty Ltd. <argot@einet.com.au> 
 *
 * This software is licensed under the Argot Public License 
 * which may be found in the file LICENSE distributed 
 * with this software.
 *
 * More information about this license can be found at
 * http://www.einet.com.au/License
 * 
 * The Developer of this software is Live Media Pty Ltd,
 * PO Box 4591, Melbourne 3001, Australia.  The license is subject 
 * to the law of Victoria, Australia, and subject to exclusive 
 * jurisdiction of the Victorian courts.
 */

package com.colony.channel;

import java.io.InputStream;
import java.io.OutputStream;

import com.argot.ResourceDictionaryLoader;
import com.argot.TypeException;
import com.argot.TypeLibrary;
import com.argot.TypeMap;
import com.argot.TypeReaderAuto;
import com.argot.auto.TypeSimpleWriter;
import com.argot.network.TypeEndPoint;
import com.colony.channel.types.ChannelInputStreamClientReader;
import com.colony.channel.types.ChannelInputStreamClientWriter;
import com.colony.channel.types.ChannelInputStreamServerReader;
import com.colony.channel.types.ChannelInputStreamServerWriter;
import com.colony.channel.types.ChannelInvalidReader;
import com.colony.channel.types.ChannelInvalidWriter;
import com.colony.channel.types.ChannelOutputStreamClientReader;
import com.colony.channel.types.ChannelOutputStreamClientWriter;
import com.colony.channel.types.ChannelOutputStreamServerReader;
import com.colony.channel.types.ChannelOutputStreamServerWriter;
import com.colony.channel.types.ChannelPipeClientReader;
import com.colony.channel.types.ChannelPipeClientWriter;
import com.colony.channel.types.ChannelPipeServerReader;
import com.colony.channel.types.ChannelPipeServerWriter;

public class ChannelLoader
extends ResourceDictionaryLoader
{
	public static final String CHANNEL_INPUT = "channel.input";
	public static final String CHANNEL_OUTPUT = "channel.output";
	public static final String CHANNEL_PIPE = "channel.pipe";

	public static final String DICTIONARY = "channel.dictionary";
	
	public ChannelLoader()
	{
		super( DICTIONARY );
	}
	
	public String getName()
	{
		return DICTIONARY;
	}
	
	public void bind( TypeLibrary library )
	throws TypeException
	{
		library.bind( ChannelIdentifier.TYPENAME, new TypeReaderAuto( ChannelIdentifier.class ), new TypeSimpleWriter(new ChannelIdentifier.ChannelIdentifierTypeWriter()), ChannelIdentifier.class );
		library.bind( CHANNEL_INPUT, new ChannelInvalidReader(), new ChannelInvalidWriter(), InputStream.class );
		library.bind( CHANNEL_OUTPUT, new ChannelInvalidReader(), new ChannelInvalidWriter(), OutputStream.class );
		library.bind( CHANNEL_PIPE, new ChannelInvalidReader(), new ChannelInvalidWriter(), TypeEndPoint.class );
		library.addClassAlias( library.getId(CHANNEL_PIPE), Channel.class);
	}
	
	public static void bindClientMap( TypeMap map, ChannelClient client )
	throws TypeException
	{
		int inputId = map.getId(CHANNEL_INPUT);
		map.setReader( inputId, new ChannelInputStreamClientReader(client));
		map.setWriter( inputId, new ChannelInputStreamClientWriter(client));
		
		int outputId = map.getId(CHANNEL_OUTPUT);
		map.setReader( outputId, new ChannelOutputStreamClientReader(client));
		map.setWriter( outputId, new ChannelOutputStreamClientWriter(client));

		int pipeId = map.getId(CHANNEL_PIPE);
		map.setReader( pipeId, new ChannelPipeClientReader(client));
		map.setWriter( pipeId, new ChannelPipeClientWriter(client));
	}
	
	public static void bindServerMap( TypeMap map, ChannelServer server )
	throws TypeException
	{
		int inputId = map.getId(CHANNEL_INPUT);
		map.setReader( inputId, new ChannelInputStreamServerReader(server));
		map.setWriter( inputId, new ChannelInputStreamServerWriter(server));
		
		int outputId = map.getId(CHANNEL_OUTPUT);
		map.setReader( outputId, new ChannelOutputStreamServerReader(server));
		map.setWriter( outputId, new ChannelOutputStreamServerWriter(server));		

		int pipeId = map.getId(CHANNEL_PIPE);
		map.setReader( pipeId, new ChannelPipeServerReader(server));
		map.setWriter( pipeId, new ChannelPipeServerWriter(server));		
	}
}
